home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 2 / Gold Medal Software Volume 2 (Gold Medal) (1994).iso / os2 / cenvi2.arj / KILL.CMD < prev    next >
OS/2 REXX Batch file  |  1993-10-11  |  3KB  |  80 lines

  1. EXTPROC CEnvi
  2. /***********************************************************************
  3.  *** Kill.cmd - CEnvi program to kill a running process by reference ***
  4.  ***            to its ID, partial name, or full name, or by full or ***
  5.  ***            partial name of it Window.                           ***
  6.  ***********************************************************************/
  7.  
  8. #include <PMdll.lib>
  9.  
  10. #define  NO_ERROR          0     // return code from most successful DosCalls
  11.  
  12. DosKillProcess(ActionCode,ProcessID)
  13.    // kill specified ProcessID using action code DKP_PRECESSTREE or DKP_PROCESS
  14. {
  15.    #define DKP_PROCESSTREE    0  // kill process and all descendents, if created by this process
  16.    #define DKP_PROCESS        1  // kill any process even if not created by this process
  17.    #define ORD_DOS32KILLPROCESS  235
  18.    return DynamicLink("doscalls",ORD_DOS32KILLPROCESS,BIT32,CDECL,ActionCode,ProcessID)
  19. }
  20.  
  21. MaybeKill(KillSpec,id,fullname)  // if KillSpec matches any part of ProcSpec, then kill
  22. {                                // the id of ProcSpec
  23.    // divide this process id into numbers and parts so we can compare
  24.    // against any one of these types
  25.    root = SplitFileName(fullname).name;
  26.    sprintf(name,"%s%s",root,SplitFileName(fullname).ext);
  27.    if ( (0 != id  &&  id == atoi(KillSpec))
  28.      || 0 == stricmp(KillSpec,fullname)
  29.      || 0 == stricmp(KillSpec,root)
  30.      || 0 == stricmp(KillSpec,name) ) {
  31.       // this id matched, and so try to kill it
  32.       rc = DosKillProcess(DKP_PROCESS,id);
  33.       if ( NO_ERROR == rc ) {
  34.          printf("%s has been killed.\n",fullname);
  35.       } else {
  36.          printf("\aError %d in DosKillProcess() from id %d\n",rc,id);
  37.       }
  38.    }
  39. }
  40.  
  41. main(argc,argv)
  42. {
  43.    if ( argc != 2  ||  !strcmp(argv[1],"/?")  ||  !strcmpi(argv[1],"help") ) {
  44.       Instructions();
  45.    } else {
  46.       // build list of all running processes
  47.       ProcList = ProcessList();
  48.       assert( NULL != ProcList );
  49.       ProcCount = 1 + GetArraySpan(ProcList);
  50.       // check each running process against this ID, whether it be name, or id
  51.       // number, or full or partial name, and try to kill it if it matches
  52.       for ( i = 0; i < ProcCount; i++ )
  53.          MaybeKill(argv[1],ProcList[i].id,ProcList[i].name);
  54.       // build list of all window titles
  55.       SwitchList = WinQuerySwitchList(Info().hab);
  56.       assert( SwitchList != NULL );
  57.       SwitchCount = 1 + GetArraySpan(SwitchList);
  58.       for ( i = 0; i < SwitchCount; i++ ) {
  59.          if ( 0 == WinQuerySwitchEntry(SwitchList[i],SwEntry) )
  60.             MaybeKill(argv[1],SwEntry.process,SwEntry.title);
  61.       }
  62.    }
  63. }
  64.  
  65. Instructions()
  66. {
  67.    printf("\n")
  68.    printf("Kill - Kill a running process by name, Window name, process ID.\n")
  69.    printf("\n")
  70.    printf("SYNTAX: Kill ProcessName | WindowTitle | ProcessID\n")
  71.    printf("\n")
  72.    printf("Where:  ProcessName - Full or partial name of running process\n")
  73.    printf("        WindowTitle - Full Window title\n")
  74.    printf("        ProcessID   - Numeric ID of the process to kill\n")
  75.    printf("\n")
  76.    printf("Example: Kill e.exe\n")
  77.    printf("\n")
  78. }
  79.  
  80.